#include "tools.h"
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h> 
/* openclose.c */
#include <fcntl.h>	/* open() and O_XXX flags */
#include <sys/stat.h>	/* S_IXXX flags */
#include <sys/types.h>	/* mode_t */
#include <unistd.h>	/* close() */
#include <stdlib.h>
#include <string.h>



//cd ..; gcc -fPIC -shared -o tools.so tools.c; mv tools.so  py/handlers/tools.so; cd py/

//char path[]  = "/home/arksm/Рабочий стол/";

Struct_data *dict_data = NULL;


int  count_file(){
    DIR *d;
    int count = 0;
    struct dirent *dir;
    d = opendir(path);
    if (d) {
    while ((dir = readdir(d)) != NULL) {
      if (strstr(dir->d_name,".desktop")!=NULL){
        count=count+1;
      }
    }
    closedir(d);
    }
    return count;
}

char* concat(char *s1, char *s2) {
    size_t len1 = strlen(s1);
    size_t len2 = strlen(s2);
    char* buff = (char *) calloc(len1 + len2 + 1, 1);
    strcat(buff, s1);
    strcat(buff, s2);
    return buff;
}


void  array_file(){
    DIR *d;
    struct dirent *dir;
    d = opendir(path);
    if (d) {
    while ((dir = readdir(d)) != NULL) { 
      file_read(dir->d_name);
    }
    closedir(d);
    }
}

void struct_memori(){
    count_f = count_file();
    if(count_f!=0){
        dict_data = (Struct_data*) malloc(count_f * sizeof(Struct_data));
    }
}


void format_data(char *text, int index){
    int len_slice = 0;
    char *value = NULL;
    char *val = NULL;
    char *name = NULL;
    int len_text = 0;
    int len_value = 0;
    val = strstr(text,searhc_simvol);
    if (val != NULL){
        len_slice = strlen(val);
        len_text = strlen(text);
        len_value = len_text-len_slice;
        value = (char*) malloc(sizeof(char)*len_slice);
        strncpy(value, val+1, len_slice);
        name = (char*) malloc(sizeof(char)*len_value);
        //name[len_slice-1] = '\0';
        strncpy(name, text, len_value);
        write_data(name, value,len_slice,index);
    }
}

void write_data(char *name, char *value,char len_slice , int index){
if (strstr (name, Version)!=NULL){
    dict_data[index].Version = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Version, value);
}
else if (strstr(name, Name_ru)!=NULL){
    dict_data[index].Name_ru = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Name_ru, value);
}
else if (strstr(name, Name)!=NULL){
    dict_data[index].Name = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Name, value);
}
else if (strstr(name, Comment_ru)!=NULL){
    dict_data[index].Comment_ru = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Comment_ru, value);
}
else if (strstr(name, Comment)!=NULL){
    dict_data[index].Comment = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Comment, value);
}
else if (strstr(name, Exec)!=NULL){
    dict_data[index].Exec = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Exec, value);
}
else if (strstr(name, Icon)!=NULL){
    dict_data[index].Icon = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Icon, value);
}
else if (strstr(name, Terminal)!=NULL){
    dict_data[index].Terminal = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Terminal, value);
}
else if (strstr(name, Type)!=NULL){
    dict_data[index].Type = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Type, value);
}
else if (strstr(name, Categories)!=NULL){
    dict_data[index].Categories = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].Categories, value);
}
else if (strstr(name, StartupNotify)!=NULL){
    dict_data[index].StartupNotify = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].StartupNotify, value);
}
else if (strstr(name, OnlyShowIn)!=NULL){
    dict_data[index].OnlyShowIn = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].OnlyShowIn, value);
}
else if (strstr(name, X_XfcePluggable)!=NULL){
    dict_data[index].X_XfcePluggable = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].X_XfcePluggable, value);
}
else if (strstr(name, X_XfceHelpPage)!=NULL){
    dict_data[index].X_XfceHelpPage = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].X_XfceHelpPage, value);
}

else if (strstr(name, X_UBLHelpPage)!=NULL){
    dict_data[index].X_UBLHelpPage = (char*) malloc(sizeof(char)*len_slice);
    strcpy(dict_data[index].X_UBLHelpPage, value);
}
// X-UBLHelpPage
}

void file_read(char *file_name)
{
    char *path_name = concat(path, file_name);
    if (strstr(file_name,".desktop")!=NULL){
        static int index = 0;
        FILE * fp;
        char * line = NULL;
        size_t len = 0;
        ssize_t read;
        fp = fopen(path_name, "r");
        if (fp == NULL)
            exit(EXIT_FAILURE);
        while ((read = getline(&line, &len, fp)) != -1) {
            format_data(line, index);
        }
        int len_path = strlen(file_name);
        dict_data[index].file_name = (char*) malloc(sizeof(char)*len_path);
        strcpy(dict_data[index].file_name,file_name);
        index+=1;
        fclose(fp);
        if (line)
            free(line);
    }
}

void freeStruct() {
    int i;
    for (i = 0; i < count_f; i++) {
        free(dict_data[i].Version);
        free(dict_data[i].Name);
        free(dict_data[i].Name_ru);
        free(dict_data[i].Comment);
        free(dict_data[i].Comment_ru);
        free(dict_data[i].Exec);
        free(dict_data[i].Icon);
        free(dict_data[i].Terminal);
        free(dict_data[i].Type);
        free(dict_data[i].Categories);
        free(dict_data[i].StartupNotify);
        free(dict_data[i].X_XfcePluggable);
        free(dict_data[i].OnlyShowIn);
        free(dict_data[i].X_XfceHelpPage);
        free(dict_data[i].X_UBLHelpPage);
    }
    free(dict_data);
}

void print_info_ru(int index){
    printf("============================================\n");
    printf("Версия %sИмя %sComment_ru %s",dict_data[index].Version,dict_data[index].Name_ru, dict_data[index].Comment_ru);
    printf("Exec %sIcon %s",dict_data[index].Exec,dict_data[index].Icon);
    printf("============================================\n");
}

void print_info_en(int index){
    printf("============================================\n");
    printf("Version %sName %sComment_ru %s",dict_data[index].Version,dict_data[index].Name, dict_data[index].Comment);
    printf("Exec %sIcon %s",dict_data[index].Exec,dict_data[index].Icon);
    printf("============================================\n");
}

void python_main(){
    struct_memori();
    array_file();
}
void execute(char * link){
    char *value = NULL;
    value = (char *) malloc(sizeof(*link));
    strncpy(value, link, strlen(link)-1);
    char *cmd = concat(value, " > /dev/null 2>&1 &");
    printf("%s", cmd);
    system(cmd);
    free(value);
}

void print_all(char *lang_flag){
    for (int i=0; i<count_f;i++){
        if (strcmp(lang_flag, "-ru")==0){
            printf("%d) %s", i, dict_data[i].Name_ru);
        }
        else if (strcmp(lang_flag, "-en")==0){
            printf("%d) %s", i, dict_data[i].Name);
        }
    }
}
int len_struct(){
    return count_f;
}

void menu(){
    int index = 0;
    scanf("%d", &index); 
    if ((index >= 0)  && (index<count_f)){
        print_info_ru(index);
    }
}

Struct_data get_data(int index){
    return dict_data[index];
}


int main(int argc, char* argv[]) {
    python_main();
    if (argc==2){
        //printf("%s",argv[1]);
        if ((strcmp(argv[1],"-ru")==0) || (strcmp(argv[1],"-en")==0)){
            print_all(argv[1]);
            menu();
        }
        else if (strcmp(argv[1],"-l")==0){
            printf("%d ", count_f);
        }
    }
    else if (argc==3){
        if ((strcmp(argv[1],"-ru")==0) || (strcmp(argv[1],"-en")==0)){
            if ((strcmp(argv[2],"-all")==0)){
                print_all(argv[1]);
                //menu();
                }
        }
        else if ((strcmp(argv[1],"-e")==0)){
                int index = 0;
                index = atoi(argv[2]);
                if ((index >= 0)  && (index<count_f)){
                    execute(dict_data[index].Exec);
                }
            }
            else {
                int index = 0;
                index = atoi(argv[2]);
                if ((index >= 0)  && (index<count_f)){
                    if (strcmp(argv[1],"-ru")==0){
                        print_info_ru(index);
                    }
                    else {
                        print_info_en(index);
                    }
                }
        }   
    }
    freeStruct();
  return 0;
}